Skip to content

Sign in with your own OpenID Connect provider when self-hosting - #451

Merged
abhishekgahlot2 merged 2 commits into
useagenthq:mainfrom
officialasishkumar:feature/oidc-sign-in
Nov 22, 2026
Merged

abhishekgahlot2 merged 2 commits into
useagenthq:mainfrom
officialasishkumar:feature/oidc-sign-in

Conversation

@officialasishkumar

@officialasishkumar officialasishkumar commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

By opening this pull request you agree to the Contributor License Agreement. On your first pull request a bot asks you to sign it with a one-line comment.

Fixes #96

A self-hosted install could offer passwords and Google, nothing else. This adds sign-in through the deployment's own OpenID Connect provider (Okta, Microsoft Entra ID, Keycloak, Authentik), set up from environment variables, with the same new-account rules as Google.

Settings

Variable Value
OIDC_ISSUER The issuer URL. Endpoints come from <OIDC_ISSUER>/.well-known/openid-configuration.
OIDC_CLIENT_ID The client id.
OIDC_CLIENT_SECRET The client secret.
OIDC_BUTTON_LABEL Optional. The button reads "Sign in with <label>"; default "SSO".
  • Redirect URI: the provider must allow <BETTER_AUTH_URL>/api/auth/oauth2/callback/oidc.
  • All or none: the first three are all set or all unset. A partial set, or an issuer that is not an http(s) URL, warns at boot and stays off, the same way the Google settings behave.
  • When absent: nothing changes. The plugin is not registered, so /api/auth/sign-in/oauth2 answers 404.

What changed

Backend

  • env.ts:
    • oidcAuthConfig reads the settings. The discovery URL drops a trailing slash before appending the well-known path, so an issuer with a path such as Keycloak's /realms/acme keeps it.
    • OIDC_PROVIDER_ID is "oidc".
    • providerSignInEnabled is Google or OpenID Connect.
  • auth.ts: when OpenID Connect is configured, it registers better-auth's genericOAuth plugin with:
    • discovery, the openid email profile scopes and PKCE
    • a mapProfileToUser with two jobs:
      • Only a literal email_verified: true marks the address verified. A missing claim or the string "true" does not.
      • The display name falls back from name to the given and family names, then preferred_username, then the address. Many providers send name only once a person has filled in their names, and the library refuses a sign-in without one.
  • New accounts: they go through the existing user.create.before hook, so the rules match Google's:
    • A pending invitation admits only a verified address.
    • Everyone else answers to signupRefusal: closed, or SIGNUP_OPEN narrowed by domain. A provider sign-in carries no invite code.
    • Linking to an existing account also needs a verified address, through the library's own rule.
    • The personal workspace and the claim takeover come from the same account hooks Google uses.
  • GET /api/auth/provider-config: answers oidc: { label } or null. The issuer and client stay on the server.
  • Other Google-only checks: canSignIn (which decides whether an invitation can ever be used) and the Slack welcome mail checked googleAuthEnabled() only. They now use providerSignInEnabled(), because an OpenID Connect sign-in links an existing address the same way. NO_WAY_IN names both providers.

Frontend

  • Sign-in card (auth-form.tsx): shows Sign in with <label> above the password form, next to Continue with Google when both are set. The "or" divider and "Sign-in is unavailable" now account for it.

  • Buttons: both use one new ProviderSignInButton. GoogleSignInButton keeps its props and markup.

  • lib/auth.ts: getAuthConfig decodes oidc (a missing or blank label reads as off), and signInWithOidc posts to /api/auth/sign-in/oauth2.

  • Desktop: the desktop sign-in page hands off through the same provider. The desktop auth client gains genericOAuthClient(), and the Electron plugin already handles /sign-in/oauth2 and /oauth2/callback/*.

  • Refused sign-ins (Google too): a refused Google or OpenID Connect sign-in used to end on better-auth's error page, which redirected to /?error=... and from there to /login with no explanation. Both web buttons now send errorCallbackURL=/login?via=provider (keeping redirect_url). The card then maps the library's code to plain words:

    • no account was created for that address (ask an admin for an invitation to a confirmed address)
    • the address has an account the provider did not confirm (use your password)
    • a generic "try again" line

    Only known codes are mapped; the query text is never shown.

Docs:

  • A "Single sign-on with OpenID Connect" section on docs-site/docs/platform/auth-and-orgs.mdx, with the Keycloak setup I ran below. The section also corrects the route that advertises sign-in methods: it is /api/auth/provider-config, not /api/config.
  • The settings in deploy/compose/env.example, compose.local.yaml and backend/README.md.

Across a release: there is no schema change.

  • An old web client ignores the new oidc field.
  • A new client on an old backend reads oidc as off and shows no button.
  • Accounts are stored under providerId: "oidc".

Tests

backend/test/oidc-auth.test.ts drives the real better-auth routes and database. A test signs in at /api/auth/sign-in/oauth2, then follows the provider's redirect back to /api/auth/oauth2/callback/oidc with the state cookie and a code. Only the identity provider is fake, stubbed at fetch (discovery and token endpoints), so nothing reaches the network. It covers:

  • Settings:
    • all three settings, the discovery URL with and without a trailing slash, and the default and trimmed label
    • partial, blank and malformed settings (no scheme, ftp:, a query, a fragment) leave it off
    • the display-name fallbacks
  • Advertising the provider:
    • provider-config answers oidc: { label: "Acme SSO" } without the secret or issuer, and null once the settings are gone
    • a server built without the settings has no /sign-in/oauth2 route (404)
  • The authorization request: it carries the client id, the openid email profile scope, an S256 code challenge and the callback redirect URI, and the code is exchanged with a PKCE verifier.
  • New accounts, in production:
    • an identity nobody invited is refused on a closed deployment, with nothing created
    • with errorCallbackURL set, the refusal lands on /login?via=provider&error=Account_creation_is_disabled
    • an invited, verified identity gets its account (address lowercased, name from preferred_username), an oidc account row, a session and its own workspace
    • an invitation does not admit email_verified: false, "true" or a missing claim
    • open sign-up refuses a domain outside SIGNUP_ALLOWED_DOMAINS and admits one inside it
  • Existing accounts:
    • an unverified identity is not linked and gets no session
    • a verified one links and signs in
    • canSignIn admits an invitation to a password-less address only while OpenID Connect is on

Frontend:

  • signup-policy.test.ts: the card with OpenID Connect only, with both providers, with no password form, and with no provider. The sign-up card stays password-only.
  • oidc-sign-in-button.test.tsx: the button's label.
  • lib/auth.test.ts: decoding oidc, and providerErrorURL.
  • verification-notice.test.ts: the provider notices, a crafted error never echoed, and error without via still meaning a confirmation link.
  • desktop-auth/page.test.ts: the desktop hand-off sends providerId: "oidc" with the desktop query and reports a refused start.

Checks with Bun 1.3.14, backend tests on pgvector/pgvector:0.8.6-pg16-bookworm:

  • bun run typecheck (root): passes
  • cd backend && bun run test: 3623 pass, 28 fail. The same 28 tests fail on an unmodified main checkout on my machine: Codex runtime and sandbox config, T3 provider bridge, and repo-prep, all spawning local processes. Then ./test/slack.test.ts: 119 pass, 0 fail.
  • cd frontend && bun run test: 2017 pass, 0 fail
  • cd frontend && bun run lint: 248 warnings and 22 infos, the same as main
  • cd frontend && bun run build: compiles, type-checks and generates every page. My machine has no Node, so Bun ran Next and crashed on exit after writing the output. The next start run below served that build.
  • cd docs-site && bunx blume validate: no broken links
  • bun test scripts/agent-docs.test.ts: 5 pass

End to end

I ran Keycloak 26.4 in Docker (start-dev) with a realm acme, a confidential client useagent and two users with verified addresses, ada and grace. Against it I ran a real backend with NODE_ENV=production on its own Postgres and a production build of the frontend (next start), with OIDC_ISSUER=http://localhost:18080/realms/acme and OIDC_BUTTON_LABEL="Acme Keycloak". The deployment was closed, and one workspace had a pending invitation for grace@acme.test. I drove it with Playwright:

  • /api/auth/provider-config answered "oidc":{"label":"Acme Keycloak"}. /login showed Sign in with Acme Keycloak above "or" and the email form.
  • Ada (not invited):
    • The button sent the browser to Keycloak's /realms/acme/protocol/openid-connect/auth.
    • After signing in there, it came back to /login?via=provider&error=Account_creation_is_disabled. The card said "This server did not create an account for that address. Ask a workspace admin to invite it. An invitation needs an address your sign-in provider has confirmed."
    • There was no session, and no user row for Ada.
  • Grace (invited):
    • She came back to /agent/new, signed in. get-session returned grace@acme.test, "Grace Hopper".
    • In the database: a user with email_verified = true, one account with provider_id = 'oidc', and "Grace Hopper's workspace" with her as owner. The invitation was still pending, to accept from its link, as after a Google sign-in.

Not run: the Electron desktop app itself. Its hand-off is covered by the unit test above, and better-auth's Electron plugin already handles the /sign-in/oauth2 and /oauth2/callback/* paths.


Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

officialasishkumar and others added 2 commits November 14, 2026 16:14
A self-hosted install could offer only passwords and Google. Companies
that run Okta, Microsoft Entra ID, Keycloak or Authentik want people to
sign in with that instead.

Backend:

- OIDC_ISSUER, OIDC_CLIENT_ID and OIDC_CLIENT_SECRET (env.ts
  oidcAuthConfig) turn on better-auth's generic OAuth provider under the
  id "oidc", with the endpoints read from the issuer's discovery
  document, the openid, email and profile scopes, and PKCE. All three or
  none: a partial set, or an issuer that is not an http(s) URL, warns
  and stays off, like the Google settings. OIDC_BUTTON_LABEL names the
  button (default "SSO"). Nothing changes when they are absent.
- New accounts go through the same user-create hook as Google: an
  invited address needs email_verified to be literally true, anyone
  else answers to the sign-up policy. Linking an existing account needs
  a verified address too. The display name falls back from name to the
  given and family names, the username, then the address, since the
  library refuses a sign-in without one.
- GET /api/auth/provider-config answers oidc: { label } or null; the
  issuer and client never leave the server.
- canSignIn and the Slack welcome mail now count any provider that can
  sign an existing address in (providerSignInEnabled), not only Google.

Frontend:

- The sign-in card shows "Sign in with <label>" above the password
  form, next to Google when both are set. The two buttons share one
  ProviderSignInButton. The desktop sign-in page hands off through the
  same provider (genericOAuthClient on the desktop auth client).
- A failed Google or OpenID Connect sign-in in the browser now returns
  to /login?via=provider instead of the library's own error page, and
  the card says why: no account was created for that address, the
  address was not confirmed for an existing account, or a generic retry
  line. Only known codes are mapped; the query text is never shown.

Docs: a "Single sign-on with OpenID Connect" section on the auth page
with a Keycloak setup, the settings in deploy/compose/env.example,
compose.local.yaml and the backend README.

Fixes useagenthq#96
@abhishekgahlot2
abhishekgahlot2 merged commit 3f450b4 into useagenthq:main Nov 22, 2026
22 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 22, 2026
@github-actions

Copy link
Copy Markdown

Thanks @officialasishkumar, merged. We added one commit on top: sign-up through a provider now needs an email the provider has verified, so an unverified claim can never hold a sign-in on someone else's address.

@github-actions github-actions Bot unlocked this conversation Nov 22, 2026
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Nov 22, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sign in with your own OpenID Connect provider when self-hosting

2 participants